function givePlayerCash(playerid, value)
{
	givePlayerMoney(playerid, value);
	pData[playerid].cash += value;
	savePlayerData(playerid, "cash");
}

function setPlayerSpawn(playerid)
{
	if(!pData[playerid].logged)
	{
		/*
		* Freeze player while loggin in
		*/	
		togglePlayerFrozen(playerid, true);
	
		/*
		* Fade out player screen
		*/	
		fadePlayerScreenOut(playerid, 0);
		return 1;
	}
	else
	{
		if(gJustDied[playerid] || gRoundStartTime > 0)
		{
			// [NOTE:] ENABLE FOR DM PURPOSE
			// [TODO:] Make more local hospitals or set spawn to default faction
			setPlayerCoordinates(playerid, -392.18, 1279.99, 23.05);
			setSpawnCamera(playerid);
			togglePlayerFrozen(playerid, true);
			gJustDied[playerid] = 0;
			return 1;
		}
		else
		{
			if(!pData[playerid].registered)
			{
				pData[playerid].registered = 1;
				savePlayerData(playerid, "registered");
			}
			// [NOTE:] ENABLE FOR DM PURPOSE
			fadePlayerScreenIn(playerid, 2);
			setCameraBehindPlayer(playerid);
			removePlayerWeapons(playerid);
			randomPlayerSpawn(playerid, pData[playerid].faction1);
			setPlayerCoordinates(playerid, pData[playerid].posX.tofloat(), pData[playerid].posY.tofloat(), pData[playerid].posZ.tofloat());
			setPlayerHeading(playerid, pData[playerid].posAngle.tofloat());
			setPlayerFactionModel(playerid);
			givePlayerFactionWeapon(playerid);
			togglePlayerFrozen(playerid, false);
			return 1;
		}		
	}
	return 1;
}

// [NOTE:] ENABLE FOR DM PURPOSE
// Prepare player for spawn
function onPlayerFactionSelect(playerid, team)
{
	switch(team)
	{
		// Team police
		case 0:
		{
			pData[playerid].faction1 = 0;
			if(pData[playerid].faction1Rank == 0 || pData[playerid].faction1Rank > 5)
			{
				pData[playerid].faction1Rank = 5;				
				savePlayerData(playerid, "faction1Rank");
			}
			savePlayerData(playerid, "faction1");
			setPlayerSpawn(playerid);
			break;
		}
		// Team gang
		case 1:
		{
			pData[playerid].faction1 = 1;
			if(pData[playerid].faction1Rank == 0 || pData[playerid].faction1Rank > 6)
			{
				pData[playerid].faction1Rank = 6;				
				savePlayerData(playerid, "faction1Rank");
			}
			savePlayerData(playerid, "faction1");
			setPlayerSpawn(playerid);
			break;
		}
	}
}
addEvent("playerFactionSelect", onPlayerFactionSelect);

// [NOTE:] ENABLE FOR DM PURPOSE
// Select random spawn pos for player in team
function randomPlayerSpawn(playerid, team)
{
	switch(gSelectedRound)
	{
		case 0:
		{
			switch(team)
			{
				// Team police
				case 0:
				{
					switch(random(0, 2))
					{
						// Spot 1
						case 0:
						{
							pData[playerid].posX = 981.597;
							pData[playerid].posY = 1870.36;
							pData[playerid].posZ = 24.0;
							pData[playerid].posAngle = 0.0;
							return;
						}
						// Spot 2
						case 1:
						{
							pData[playerid].posX = 991.591;
							pData[playerid].posY = 1874.58;
							pData[playerid].posZ = 24.3;
							pData[playerid].posAngle = 0.0;
							return;
						}
						// Spot 3
						case 2:
						{
							pData[playerid].posX = 979.879;
							pData[playerid].posY = 1895.54;
							pData[playerid].posZ = 23.9;
							pData[playerid].posAngle = 0.0;
							return;
						}
					}
					return;
				}
				// Team gang
				case 1:
				{
					switch(random(0, 2))
					{
						// Spot 1
						case 0:
						{
							pData[playerid].posX = 1069.67;
							pData[playerid].posY = 1855.76;
							pData[playerid].posZ = 12.8766;
							pData[playerid].posAngle = 0.0;
							return;
						}
						// Spot 2
						case 1:
						{
							pData[playerid].posX = 1118.69;
							pData[playerid].posY = 1849.81;
							pData[playerid].posZ = 12.0452;
							pData[playerid].posAngle = 0.0;
							return;
						}
						// Spot 3
						case 2:
						{
							pData[playerid].posX = 1033.72;
							pData[playerid].posY = 1789.15;
							pData[playerid].posZ = 13.5367;
							pData[playerid].posAngle = 0.0;
							return;
						}
					}
					return;
				}
			}
			return;
		}
	}
}

function setPlayerFactionModel(playerid)
{
	// [NOTE:] ENABLE FOR DM PURPOSE
	switch(pData[playerid].faction1Rank)
	{
		case 1: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin1);  break; }
		case 2: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin2);  break; }
		case 3: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin3);  break; }
		case 4: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin4);  break; }
		case 5: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin5);  break; }
		case 6: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin6);  break; }
		case 7: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin7);  break; }
		case 8: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin8);  break; }
		case 9: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin9);  break; }
		case 10: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin10);  break; }
		case 11: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin11);  break; }
		case 12: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin12);  break; }
		case 13: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin13);  break; }
		case 14: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin14);  break; }
		case 15: { setPlayerModel(playerid, fData[pData[playerid].faction1].skin15);  break; }
	}
}

// [NOTE:] ENABLE FOR DM PURPOSE
function givePlayerFactionWeapon(playerid)
{
	switch(pData[playerid].faction1)
	{
		// Team police
		case 0:
		{
			switch(pData[playerid].faction1Rank)
			{
				case 1:
				{					
					givePlayerWeapon(playerid, 13, 100);
					givePlayerWeapon(playerid, 15, 100);
					break;
				}
				case 2:
				{					
					givePlayerWeapon(playerid, 10, 8);
					givePlayerWeapon(playerid, 13, 100);
					break;
				}
				case 3:
				{					
					givePlayerWeapon(playerid, 7, 25);
					givePlayerWeapon(playerid, 13, 100);
					break;
				}
				case 4:
				{					
					givePlayerWeapon(playerid, 7, 35);
					givePlayerWeapon(playerid, 10, 8);
					break;
				}
				case 5:
				{					
					givePlayerWeapon(playerid, 7, 35);
					break;
				}
			}
			break;
		}
		// Team gang
		case 1:
		{
			switch(pData[playerid].faction1Rank)
			{
				case 1:
				{
					givePlayerWeapon(playerid, 12, 200);
					givePlayerWeapon(playerid, 14, 100);
					break;
				}
				case 2:
				{
					givePlayerWeapon(playerid, 9, 25);
					givePlayerWeapon(playerid, 12, 150);
					break;
				}
				case 3:
				{
					givePlayerWeapon(playerid, 9, 25);
					givePlayerWeapon(playerid, 11, 8);
					break;
				}
				case 4:
				{
					givePlayerWeapon(playerid, 3, 1);
					givePlayerWeapon(playerid, 7, 100);
					break;
				}
				case 5:
				{
					givePlayerWeapon(playerid, 3, 1);
					givePlayerWeapon(playerid, 7, 35);
					break;
				}
			}
			break;
		}
	}
}

// [NOTE:] ENABLE FOR DM PURPOSE
function setSpawnCamera(playerid)
{
	switch(gSelectedRound)
	{
		case 0:
		{
			switch(pData[playerid].faction1)
			{
				// Team police
				case 0:
				{
					setPlayerCameraPos(playerid, 945.143, 1883.18, 50.0);
					setPlayerCameraLookAt(playerid, 990.0, 1852.8, 15.0);
					return;
				}
				// Team gang
				case 1:
				{
					setPlayerCameraPos(playerid, 1037.23, 1842.59, 39.5);
					setPlayerCameraLookAt(playerid, 1075.16, 1823.68, 14.0);
					return;
				}
			}
			return;
		}
	}
}

function onUpdatePlayersStats(playerid)
{
	local lPlayersKills = {};
	local lPlayersDeaths = {};
	local lPlayersPoints = {};
	local lPlayersFactions = {};
	local lPlayersCount = 0;
	foreach(i, playername in getPlayers())
	{
		lPlayersKills[i] <- pData[i].kills;
		lPlayersDeaths[i] <- pData[i].deaths;
		lPlayersPoints[i] <- pData[i].points;
		lPlayersFactions[i] <- pData[i].faction1;
		lPlayersCount++;
	}
	triggerClientEvent(playerid, "playerStatsUpdate", lPlayersKills, lPlayersDeaths, lPlayersPoints, lPlayersFactions, lPlayersCount, fData[0].kills, fData[1].kills, fData[2].kills);
}
addEvent("updatePlayersStats", onUpdatePlayersStats);

function onPlayerBuyWeapon(playerid, weaponid, ammo)
{
	switch(weaponid)
	{
		case 1:
		{
			if(pData[playerid].cash > 15)
			{
				givePlayerCash(playerid, -15);
				triggerClientEvent(playerid, "weaponBuy", "You've bought bat", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 2:
		{
			if(pData[playerid].cash > 10)
			{
				givePlayerCash(playerid, -10);
				triggerClientEvent(playerid, "weaponBuy", "You've bought pool cue.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 3:
		{
			if(pData[playerid].cash > 20)
			{
				givePlayerCash(playerid, -20);
				triggerClientEvent(playerid, "weaponBuy", "You've bought knife.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 7:
		{
			if(pData[playerid].cash > 120)
			{
				givePlayerCash(playerid, -120);
				triggerClientEvent(playerid, "weaponBuy", "You've bought pistol with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 9:
		{
			if(pData[playerid].cash > 160)
			{
				givePlayerCash(playerid, -160);
				triggerClientEvent(playerid, "weaponBuy", "You've bought combat pistol with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 12:
		{
			if(pData[playerid].cash > 220)
			{
				givePlayerCash(playerid, -220);
				triggerClientEvent(playerid, "weaponBuy", "You've bought micro SMG with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 13:
		{
			if(pData[playerid].cash > 280)
			{
				givePlayerCash(playerid, -280);
				triggerClientEvent(playerid, "weaponBuy", "You've bought SMG with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 10:
		{
			if(pData[playerid].cash > 260)
			{
				givePlayerCash(playerid, -260);
				triggerClientEvent(playerid, "weaponBuy", "You've bought pump shotgun with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 11:
		{
			if(pData[playerid].cash > 300)
			{
				givePlayerCash(playerid, -300);
				triggerClientEvent(playerid, "weaponBuy", "You've bought combat shotgun with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 14:
		{
			if(pData[playerid].cash > 350)
			{
				givePlayerCash(playerid, -350);
				triggerClientEvent(playerid, "weaponBuy", "You've bought assault rifle with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 15:
		{
			if(pData[playerid].cash > 400)
			{
				givePlayerCash(playerid, -400);
				triggerClientEvent(playerid, "weaponBuy", "You've bought carbine rifle with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 17:
		{
			if(pData[playerid].cash > 800)
			{
				givePlayerCash(playerid, -800);
				triggerClientEvent(playerid, "weaponBuy", "You've bought sniper rifle rifle with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
		case 16:
		{
			if(pData[playerid].cash > 1000)
			{
				givePlayerCash(playerid, -1000);
				triggerClientEvent(playerid, "weaponBuy", "You've bought combat sniper rifle rifle with " + ammo + " rounds.", pData[playerid].cash);
			}
			else
			{
				triggerClientEvent(playerid, "weaponBuy", "You don't have enough money to buy this weapon.", pData[playerid].cash);
				return;
			}
			break;
		}
	}
	givePlayerWeapon(playerid, weaponid, ammo);
	togglePlayerHud(playerid, true);
}
addEvent("playerBuyWeapon", onPlayerBuyWeapon);

function onPlayerUnfreeze(playerid)
{
	togglePlayerFrozen(playerid, false);
}
addEvent("playerUnfreeze", onPlayerUnfreeze);